package jjn.carl.stack_queue;

import java.util.*;

/**
 * @author Jiang Jining
 * @since 2023-07-09 11:39
 */
public class LeetCode20 {
    public boolean isValid(String s) {
        if (s == null) {
            return false;
        }
        Deque<Character> cur = new ArrayDeque<>();
        Map<Character, Character> map = new HashMap<>();
        map.put('}', '{');
        map.put(')', '(');
        map.put(']', '[');
        for (int i = 0; i < s.length(); i++) {
            if (cur.isEmpty() || !Objects.equals(cur.peekLast(), map.get(s.charAt(i)))) {
                cur.offer(s.charAt(i));
            } else {
                cur.pollLast();
            }
        }
        return cur.isEmpty();
    }
    
    public static void main(String[] args) {
//        System.out.println("new LeetCode20().isValid(\"()[]{}\") = " + new LeetCode20().isValid("()[]{}"));
        System.out.println("new LeetCode20().isValid(\"{[]}\") = " + new LeetCode20().isValid("{[]}"));
    }
}
